有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java组合框如何添加图标?

我是FXML新手,正在开发一个应用程序。 现在我遇到了一个我无法解决的问题

我在FXML中定义了一个组合框,并在controller类中创建了necesarry关联。 但我想在这个组合框中添加图像

在谷歌上搜索了几个小时后,我仍然无法修复这个问题

你们能帮我举个“简单”的例子来说明如何达到我的目标吗

非常感谢

我目前的代码是:(当然有一种更简单的方法可以做到这一点,但它是有效的!)

ImageView img1 = new ImageView("Layout/nl.png");
ImageView img2 = new ImageView("Layout/en.png");

AnimalBoxLanguage.getItems().addAll(img1, img2);

AnimalBoxLanguage.setCellFactory(new Callback<ListView<ImageView>, ListCell<ImageView>>() {
    @Override 
    public ListCell<ImageView> call(ListView<ImageView> p) {
        return new ListCell<ImageView>() {
            private final ImageView rectangle;
            { 
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
                rectangle = new ImageView();
            }

            @Override 
            protected void updateItem(ImageView item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setGraphic(null);
                } else {
                    rectangle.setImage(item.getImage());
                    setGraphic(rectangle);
                }
            }
        };
    }
});

共 (3) 个答案

  1. # 1 楼答案

    以他评论中链接的sampleerhun为起点,在fxml中定义ComboBox,如下所示,这样ComboBox项就包括带有图形的标签(这些是你的“图标”)

    <ComboBox fx:id="fruitCombo" layoutX="15.0" layoutY="33.0" prefWidth="90.0" promptText="choose">
      <items>
        <FXCollections fx:factory="observableArrayList">
          <Label text="Apple">
            <graphic> 
              <StackPane prefWidth="50">
                <ImageView fitHeight="32" preserveRatio="true">
                  <image>
                    <Image url="http://uhallnyu.files.wordpress.com/2011/11/green-apple.jpg" preserveRatio="false" smooth="false" />
                  </image>
                </ImageView>
              </StackPane>  
            </graphic>  
          </Label>  
          <Label text="Pear">
            <graphic> 
              <StackPane prefWidth="50">
                <ImageView fitHeight="32" preserveRatio="true">
                  <image>
                    <Image url="http://smoothiejuicerecipes.com/pear.jpg" preserveRatio="false" smooth="false" />
                  </image>
                </ImageView>
              </StackPane>  
            </graphic>  
          </Label>  
          <Label text="Orange">
            <graphic> 
              <StackPane prefWidth="50">
                <ImageView fitHeight="32" preserveRatio="true">
                  <image>
                    <Image url="http://i.i.com.com/cnwk.1d/i/tim/2011/03/10/orange_iStock_000001331357X_540x405.jpg" preserveRatio="false" smooth="false" />
                  </image>
                </ImageView>
              </StackPane>  
            </graphic>  
          </Label>  
        </FXCollections>
      </items>
    </ComboBox>
    

    在FructureComboController initialize方法中,为按钮设置一个自定义单元格(下面的简单单元格仅获取所选项目的文本,但如果您愿意,也可以包含一个图形):

    ListCell<Label> buttonCell = new ListCell<Label>() {
      @Override protected void updateItem(Label item, boolean isEmpty) {
        super.updateItem(item, isEmpty);
        setText(item == null ? "" : item.getText());        
      }
    };
    fruitCombo.setButtonCell(buttonCell);
    

    以上只是一种方法。或者(也许更好)你可以为你的ComboBox定义一个细胞工厂,正如塞巴斯蒂安在他的回答中指出的那样

    修改样本的输出:

    iconcombosample

  2. # 2 楼答案

    ObservableList options = FXCollections.observableArrayList();
    //
    Image img = new Image("/images/auto32.png");
    ImageView imgView = new ImageView();
    imgView.setImage(img);
    //
    Label lbl = new Label("test");
    lbl.setGraphic(imgView);
    // 
    options.add(lbl);
    options.add(lbl);
    options.add(lbl);
    //
    myCombo.setItems(options);
    
  3. # 3 楼答案

    您需要自定义组合框的CellFactory以保持框中项目的可视化。请参见this link以获取一个简短的示例

    要使图像在控制区域中可见(选择一个项目后),必须将组合框的按钮单元格设置为其中一个单元格。JavaFX将自动相应地更新它们。基本上,你需要做的是,当你用你的定制cellfactory设置组合框时:

    mycombobox.setButtonCell(myCellFactory.call(null));
    

    也可以看看关于这个的documentation